package com.linkallcloud.web.processor;

import java.lang.reflect.Method;
import java.lang.reflect.Type;

import com.alibaba.fastjson2.JSON;
import com.linkallcloud.core.face.message.FaceMessage;
import com.linkallcloud.core.lang.Strings;
import com.linkallcloud.deapi.DeApiException;
import com.linkallcloud.deapi.DeRequest;
import com.linkallcloud.deapi.annotation.DeApi;
import com.linkallcloud.deapi.tuils.Encrypts;

public class DeApiRequestProcessor extends BaseProcessor {

    private String appCode;
    private String appSecret;

    public DeApiRequestProcessor(String appCode, String appSecret) {
        super();
        this.appCode = appCode;
        this.appSecret = appSecret;
    }

    @Override
    protected FaceMessage doConvert2RealRequest(String messagePkg, Type faceType, Method method) throws Exception {
        DeRequest dr = JSON.parseObject(messagePkg, DeRequest.class);
        DeApi faceAnno = method.getAnnotation(DeApi.class);
        if (dr != null && faceAnno != null) {
            verifyDeRequest(dr);

            String content = dr.getContent();
            if (!Strings.isBlank(content)) {
                // api指定了算法
                if (!Strings.isBlank(faceAnno.encrypt())) {
                    // 双方算法标识一致
                    if (!Strings.isBlank(dr.getEncrypt()) && faceAnno.encrypt().equalsIgnoreCase(dr.getEncrypt())) {
                        // AES
                        if ("AES".equalsIgnoreCase(dr.getEncrypt())) {
                            try {
                                content = Encrypts.decryptAES(content, appSecret);
                            } catch (Exception e) {
                            }
                        }
                        // 其它算法由api自行处理
                    } else {
                        throw new DeApiException("error_encrypt", "encrypt标识与能力接口要求的不一致");
                    }
                } else {// api未指定算法
                    // 数据包指定了AES，则使用能力底座AES算法
                    if (!Strings.isBlank(dr.getEncrypt()) && "AES".equalsIgnoreCase(dr.getEncrypt())) {
                        try {
                            content = Encrypts.decryptAES(content, appSecret);
                        } catch (Exception e) {
                        }
                    }
                }
                return JSON.parseObject(content, faceType);
            }
        }
        return null;
    }

    private void verifyDeRequest(DeRequest msg) throws DeApiException {
        if (msg == null) {
            throw new DeApiException("error_message", "数据包为空");
        }
        if (Strings.isBlank(msg.getReceiver()) || !msg.getReceiver().equals(appCode)) {
            throw new DeApiException("error_receiver", "receiver参数错误");
        }

        // ip/域名白名单验证
        // if (Strings.isBlank(domain) || Strings.isBlank(requestApp.getIpWhiteList())
        // || requestApp.getIpWhiteList().indexOf(domain) == -1) {
        // throw new DeApiException("error_ip_whitelist", "ip地址未在白名单中配置");
        // }

        // 协议和签名验证
        msg.verify(appSecret);
    }

}
